package com.study.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {
    // 化繁为简, 先完成显示菜单
    public static void main(String[] args) {
        boolean loop = true;
        Scanner sc = new Scanner(System.in);
        String key = "";

        // 零钱通明细
        String details = "--------零钱通明细-------";

        // 完成收益入账
        double money = 0;
        double balance = 0;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 消费
        String note = "";

        do {
            System.out.println("======零钱通菜单=======");
            System.out.println("\t1 零钱通明细");
            System.out.println("\t2 收益入账");
            System.out.println("\t3 消费");
            System.out.println("\t4 退出");

            System.out.println("请选择1~4: ");
            key = sc.next();

            // 分支控制
            switch (key) {
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.println("收益入账:");
                    money = sc.nextDouble();
                    // 校验 money
                    if (money <= 0) {
                        System.out.println("入账不能为负数");
                        break;
                    }
                    balance += money;
                    date = new Date();
                    details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "3":
                    System.out.println("消费金额:");
                    money = sc.nextDouble();
                    if (money <= 0 || money > balance) {
                        System.out.println("消费金额应在0-" + balance + "之间");
                        break;
                    }
                    System.out.println("消费说明:");
                    note = sc.next();
                    balance -= money;
                    date = new Date();
                    details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "4":
                    String choice = "";
                    while (true) {
                        System.out.println("你确定要退出吗? y/n");
                        choice = sc.next();
                        if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")) {
                            break;
                        }
                    }

                    if (choice.equalsIgnoreCase("y")) {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("选择有误, 请重新选择");
            }
        } while (loop);

        System.out.println("======退出零钱通项目=======");
    }
}
